/**
 * 
 */
package edu.gatech.cuckoo;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

/**
 * @author Dale Avery
 *
 */
public class GenericHashFunctionTest {
	
	GenericHashFunction<Integer, String> function;
	GenericHashFunction<Integer, String> function2;
	MapEntry<Integer, String> dale;
	MapEntry<Integer, String> hailin;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		function = new GenericHashFunction<Integer, String>(0);
		function2 = new GenericHashFunction<Integer, String>(0);
		dale = new MapEntry<Integer, String>(22, "Dale");
		hailin = new MapEntry<Integer, String>(18, "Hailin");
	}

	/**
	 * Test method for {@link edu.gatech.cuckoo.GenericHashFunction#hashCode(java.util.Map.Entry)}.
	 */
	@Test
	public void testHashCodeEntryOfKV() {
		int resultDale;
		int resultDale1;
		int resultHailin;
		int resultHailin1;
		
		// Hash codes should return the same value if their factors are the same.
		setFactors(1, 1);
		resultDale = function.hashCode(dale.getKey());
		resultDale1 = function2.hashCode(dale.getKey());
		resultHailin = function.hashCode(hailin.getKey());
		resultHailin1 = function2.hashCode(hailin.getKey());
		
		assertTrue(resultDale == resultDale1);
		assertTrue(resultHailin == resultHailin1);
		
		// Hash codes should differ now.
		setFactors(1, 2);
		resultDale = function.hashCode(dale.getKey());
		resultDale1 = function2.hashCode(dale.getKey());
		resultHailin = function.hashCode(hailin.getKey());
		resultHailin1 = function2.hashCode(hailin.getKey());
		
		assertTrue(resultDale != resultDale1);
		assertTrue(resultHailin != resultHailin1);
	}
	
	private void setFactors(int factor1, int factor2) {
		function.setFactor(factor1);
		function2.setFactor(factor2);
	}

}
